//格式化时间
//dateTime：时间戳
function getFormtTime(dateTime,flag) {
    if(dateTime != null ) {
        //若传入的dateTime为字符串类型，需要进行转换成数值，若不是无需下面注释代码
        var time = parseInt(dateTime)
        var date = new Date(time);
        //获取年份
        var YY = date.getFullYear();
        //获取月份
        var MM = (date.getMonth() + 1 < 10 ? '0'+(date.getMonth() + 1) : date.getMonth() + 1);
        //获取日期
        var DD = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate());
        if(flag) { //flag为true，显示时分秒格式
            //获取小时
            var hh = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours());
            //获取分
            var mm = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes());
            ///获取秒
            var ss = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
            //返回时间格式： 2020-11-09 13:14:52
            return YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
        } else {
            //返回时间格式： 2020-11-09
            return YY + '-' + MM + '-' + DD;
        }
    } else {
        return "";
    }
}


let id;
$(document).ready(function (){

    queryByCondition();

});

/**
 * 查询单个数据通过多个条件模糊查询
 */
// function queryType(){
//
//     $.ajax({
//         url:"/product/queryType",
//         type:"post",
//         data:{},
//         dataType:"json",
//         success:function (result){
//             var str = "<option selected='selected' value='0'>请选择</option>";
//             for (var i = 0; i < result.length; i++) {
//                 str += "<option value='" + result[i].brandId + "'>"+result[i].typeName+"</option>";
//             }
//             $("#ptype").html(str);
//         }
//     })
// }
// let id;
function queryBrand(){

    // var id = $("#type option:selected").val();
    $.ajax({
        url:"/productType/queryBrand",
        type:"post",
        data:{},
        dataType:"json",
        success:function (result){
            var str = "<option selected='selected' value='0'>请选择</option>";
            for (var i = 0; i < result.length; i++) {
                str += "<option>"+result[i].brandName+"</option>";
            }
            $("#brand").html(str);
        }
    })
}
function queryByCondition(){
     id = window.sessionStorage.getItem('id');
    var bName = window.sessionStorage.getItem('bName');
    var tName = window.sessionStorage.getItem('tName');
    var status = window.sessionStorage.getItem('status');
    var creatTime = window.sessionStorage.getItem('creatTime');
    var creatPeople = window.sessionStorage.getItem('creatPeople');
    var ck = window.sessionStorage.getItem('ck');
    $.ajax({
        url:"/productType/queryBrand",
        type:"post",
        data:{},
        dataType:"json",
        success:function (result){
            var str = "";
            if (status=="可用"){
                str +=  "    <li>\n" +
                    "      <label>品牌名称</label>\n" +
                    "      <select class=\"dfselect\" id='bName'>\n" +
                    "          <option >请选择</option>\n" +
                    "          <option selected=\"selected\">"+bName+"</option>\n";
                var st = "";
                for (var i = 0; i < result.length; i++) {
                    if (bName==result[i].brandName){
                        continue;
                    }else {
                        st += "<option>"+result[i].brandName+"</option>"
                    }
                };
                str += st;
                str +="</select>\n" +
                    "      <i>必选</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>商品类型名称</label>\n" +
                    "      <input name=\"\" type=\"text\" value='"+tName+"' class=\"dfinput\" id='tName' />\n" +
                    "      <i>必填，不能超过50个字符</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>状态</label>\n" +
                    "      <select class=\"dfselect\" id='status'>\n" +
                    "          <option value=\"0\" >不可用</option>\n" +
                    "          <option value=\"1\" selected>可用</option>\n" +
                    "        </select>\n" +
                    "      <i>必选</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>创建人</label>\n" +
                    "      <input name=\"\" type=\"text\" value='"+creatTime+"' readonly=\"readonly\" class=\"roinput\" id='cPeople' />\n" +
                    "      <i>不能编辑</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>创建时间</label>\n" +
                    "      <input name=\"\" type=\"text\" value='"+creatPeople+"' readonly=\"readonly\" class=\"roinput\" id='cTime' />\n" +
                    "      <i>不能编辑</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>&nbsp;</label>\n" +
                    "      <input name=\"\" type=\"button\" class=\"btn\" value=\"确定\" onclick='updateType()'/>\n" +
                    "      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n" +
                    "      <input name=\"\" type=\"button\" class=\"btn\" value=\"返回\" onclick=\"window.location.href='productTypeList.html'\"/>\n" +
                    "    </li>";
                $(".forminfo").html(str);
            }
            else if (status=="不可用"){
                str +=  "    <li>\n" +
                    "      <label>品牌名称</label>\n" +
                    "      <select class=\"dfselect\" id='bName'>\n" +
                    "          <option >请选择</option>\n" +
                    "          <option selected=\"selected\">"+bName+"</option>\n";
                var st = "";
                for (var i = 0; i < result.length; i++) {
                    if (bName==result[i].brandName){
                        continue;
                    }else {
                        st += "<option>"+result[i].brandName+"</option>"
                    }
                };
                str += st;
                    str +="</select>\n" +
                    "      <i>必选</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>商品类型名称</label>\n" +
                    "      <input name=\"\" type=\"text\" value='"+tName+"' class=\"dfinput\" id='tName' />\n" +
                    "      <i>必填，不能超过50个字符</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>状态</label>\n" +
                    "      <select class=\"dfselect\" id='status'>\n" +
                    "          <option value=\"0\" selected>不可用</option>\n" +
                    "          <option value=\"1\" >可用</option>\n" +
                    "        </select>\n" +
                    "      <i>必选</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>创建人</label>\n" +
                    "      <input name=\"\" type=\"text\" value='"+creatTime+"' readonly=\"readonly\" class=\"roinput\" id='cPeople' />\n" +
                    "      <i>不能编辑</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>创建时间</label>\n" +
                    "      <input name=\"\" type=\"text\" value='"+creatPeople+"' readonly=\"readonly\" class=\"roinput\" id='cTime' />\n" +
                    "      <i>不能编辑</i>\n" +
                    "    </li>\n" +
                    "    <li>\n" +
                    "      <label>&nbsp;</label>\n" +
                    "      <input name=\"\" type=\"button\" class=\"btn\" value=\"确定\" onclick='updateType()'/>\n" +
                    "      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n" +
                    "      <input name=\"\" type=\"button\" class=\"btn\" value=\"返回\" onclick=\"window.location.href='productTypeList.html'\"/>\n" +
                    "    </li>";
                $(".forminfo").html(str);
            }
        }
    })
}



function updateType(){

    var bName = $("#bName option:selected").text();
    var tName = $("#tName").val();
    var status = $("#status option:selected").val();

    var tId =id;
    $.ajax({
        url:"/productType/updateType",
        type:"post",
        data:{tId:tId,bName:bName,tName:tName,status:status},
        dataType:"json",
        success:function (result){
            if (result=="true"){
                alert("修改成功");
                window.location="productTypeList.html";
            }else if (result=="false"){
                alert("修改失败");
            }else if (result=="falied"){
                alert("修改的商品类型不存在，请重新修改");
            }
        }
    })
}